﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCon;

namespace yfxt
{
    public partial class gl_dbck : Form
    {
        public gl_dbck()
        {
            InitializeComponent();
        }

        public string sqyf;
        public string shenqingdanhao2;       //传值比 确认按钮 窗体
        string comStr;
        int queren_var = 0;               // 变量，控制“确认”按钮是否执行代码
        int tuihui_var = 0;               // 变量，控制“退回”按钮是否执行代码

        /// <summary>
        /// 刷新函数 ---显示左和右DatagridView
        /// </summary>
        private void xianshi_DatagridView()
        {
            //左边DataGridView
            DataTable dt1 = new DataTable();
            comStr = "select sqdh,sqrq,bzxx " +
                "from YF_DB01 where tjbz=1 and ckbz=0 and mbyf='" + mainform.yfsb + "' and sqyf=(select yfsb from yf_yflb where yfmc='" + comboBox1.Text + "')";
            dt1.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView1.Rows.Clear();
            if (dt1.Rows.Count > 0)
            {
                dataGridView1.Rows.Add(dt1.Rows.Count);
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = dt1.Rows[i][0].ToString();
                    dataGridView1.Rows[i].Cells[1].Value = Convert.ToDateTime(dt1.Rows[i][1]).ToString("yyyy-MM-dd").Replace('-', '.');
                    dataGridView1.Rows[i].Cells[2].Value = dt1.Rows[i][2].ToString();
                }
            }

            //右边DataGridView
            DataTable dt2 = new DataTable();
            comStr = "select sqdh,ckrq,bzxx " +
                "from YF_DB01 where tjbz=1 and ckbz=1 and sqyf='" + mainform.yfsb + "' and sqyf=(select yfsb from yf_yflb where yfmc='" + comboBox1.Text + "')";
            dt2.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView2.Rows.Clear();
            if (dt2.Rows.Count > 0)
            {
                dataGridView2.Rows.Add(dt2.Rows.Count);
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = dt1.Rows[i][0].ToString();
                    dataGridView1.Rows[i].Cells[1].Value = Convert.ToDateTime(dt1.Rows[i][1]).ToString("yyyy-MM-dd").Replace('-', '.');
                    dataGridView1.Rows[i].Cells[2].Value = dt1.Rows[i][2].ToString();
                }
            }

            #region datagridView默认不选中单元格，使之成蓝色
            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.Rows[0].Selected = false;
            }
            if (dataGridView2.Rows.Count > 0)
            {
                dataGridView2.Rows[0].Selected = false;
            }
            #endregion
        }

        /// <summary>
        /// 刷新按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            xianshi_DatagridView();
        }

        /// <summary>
        /// 确认按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            shenqingdanhao2 = dataGridView2.CurrentRow.Cells[0].Value.ToString();
            gl_dbckd_zuo dbckd_zuo = new gl_dbckd_zuo();
            dbckd_zuo.Owner = this;
            sqyf = comboBox1.Text.ToString();
            dbckd_zuo.ShowDialog();
        }

        /// <summary>
        /// 退回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                if (MessageBox.Show("是否退回调拨申请单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "？",
                    "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    try
                    {
                        comStr = "update YF_DB01 set tjbz='2' where sqdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                        SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                        xianshi_DatagridView();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
            }
        }

        /// <summary>
        /// 退出按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gl_dbck_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToResizeRows = false;                     //禁止用鼠标调整datagridView单元格的大小。
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView2.AllowUserToResizeRows = false;                     //禁止用鼠标调整datagridView单元格的大小。
            dataGridView2.AllowUserToResizeColumns = false;
            foreach (DataGridViewColumn c in dataGridView1.Columns)        //禁止对datagridView列进行排序
            {
                c.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            foreach (DataGridViewColumn c in dataGridView2.Columns)        //禁止对datagridView列进行排序
            {
                c.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            //目标药房
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            DataTable dt = new DataTable();
            comStr = "select yfmc from yf_yflb where yfsb!=" + mainform.yfsb + " and yfsb!=2";
            dt.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                comboBox1.Items.Add(dt.Rows[i][0]);
            }
            comboBox1.Text = dt.Rows[0][0].ToString();

            xianshi_DatagridView();
        }

        #region 显示调拨申请单
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            shenqingdanhao2 = dataGridView1.CurrentRow.Cells[0].Value.ToString();            
            gl_dbckd_zuo dbckd_zuo = new gl_dbckd_zuo();
            dbckd_zuo.Owner = this;
            sqyf = comboBox1.Text.ToString();
            dbckd_zuo.ShowDialog();
        }

        private void dataGridView2_KeyDown(object sender, KeyEventArgs e)
        {
            shenqingdanhao2 = dataGridView2.CurrentRow.Cells[0].Value.ToString();
            gl_dbckd_you dbckd_you = new gl_dbckd_you();
            dbckd_you.Owner = this;
            dbckd_you.ShowDialog();
        }
        #endregion

        //变量，控制“提交”按钮是否执行代码
        private void dataGridView1_Enter(object sender, EventArgs e)
        {
            queren_var = 1;
        }

        #region datagridView失去焦点，单元格不显示蓝色
        private void dataGridView1_Leave(object sender, EventArgs e)
        {
            if (!button5.Focused)
            {
                queren_var = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {

                    dataGridView1.Rows[i].Selected = false;
                }
            }

            //方法2：
            //this.dataGridView1.ClearSelection();
            //this.dataGridView1.TabStop = false;

            //方法3：this.dataGridView1.CurrentCell = null;
        }

        private void dataGridView2_Leave(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {

                dataGridView2.Rows[i].Selected = false;
            }
        }
        #endregion

        //窗体重新获取焦点，发生的刷新事件
        private void gl_dbck_Paint(object sender, PaintEventArgs e)
        {
            xianshi_DatagridView();
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            xianshi_DatagridView();
        }
    }
}
